<?php
session_start();
//error_reporting(0);
DEFINE("FRAMEWORK_PATH", dirname(dirname( __FILE__ )) ."/" );
include 'facebook-php-sdk-2343fca/src/facebook.php';
include 'FBClass.php';
include FRAMEWORK_PATH . 'controllers/profile/profileinformationcontroller.php';
?>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-personalized-1.5.2.packed.js"></script>
<script type="text/javascript" src="js/sprinkle.js"></script>

<?php
	
	$fb = new FB();
	
	//get user id and profile type of the profile 
	// and add to redirect URL
	$user_id = "";
	$profile_type = "";
	if(isset($_GET['user_id']) ){
   		
		$user_id = $_GET['user_id'];
   	}
   	if(isset($_GET['profile_id']) ){
   		
   		$profile_type = $_GET['profile_id'];
   	}
   	
    $app_id = '257478380945147';
    $app_secret = "5d55c36e1ce492427ea9b870c6b6cd76";
    $my_url = "http://duminda.net76.net/profile/home/".$user_id."/".$profile_type."/facebook";
  //$my_url = "http://duminda.net76.net/profile/home/1/5/facebook";
    $code = $_REQUEST["code"];

    if(empty($code)) {
        $dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
            . $app_id . "&redirect_uri=" . urlencode($my_url)."&scope=email,read_stream,publish_stream,offline_access";

        echo("<script> top.location.href='" . $dialog_url . "'</script>");
    }

    $token_url = "https://graph.facebook.com/oauth/access_token?client_id="
        . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
        . $app_secret . "&code=" . $code;

    $access_token = file_get_contents($token_url);
    $graph_url = "https://graph.facebook.com/me?" . $access_token;
    $user = json_decode(file_get_contents($graph_url));

	print_r($user->id);
	
	
	
	$query = "https://api.facebook.com/method/fql.query?query=";
	$query .= urlencode("SELECT post_id, actor_id, target_id, message,comments,likes FROM stream WHERE filter_key in (SELECT filter_key FROM stream_filter WHERE uid=me() AND type='newsfeed') AND is_hidden = 0"); 

	$query .= "&".$access_token."&format=json";
	$out = file_get_contents($query);
	$response = json_decode($out);

	
	echo '<pre>';print_r($response);echo '</pre>';
	
	
	//echo "<br/>".$access_token."<br/>";
	
	/********************************************************/
		$facebook = new Facebook(array(
		'appId' => '257478380945147',
		'secret' => '5d55c36e1ce492427ea9b870c6b6cd76',
		'cookie' => true,
		'code' => $code,
		));
		echo "<img src='https://graph.facebook.com/".$user->id."/picture'";
		print_r($user->name);
		
		//$me = $facebook->api('/me');
		//print_r($me);
		//$fb->post_news($me['id'],$access_token);
		//echo("ID " . $me['id']);

		?>
			<form method="post" action="index.php">
			<textarea name="status"></textarea>
			<input type="Submit" value="Update"/>
			</form>
	    <?php
		if($_SERVER["REQUEST_METHOD"] == "POST"){
			
			$status = $_POST['status'];
			$statusUpdate = $facebook->api('/'.$user->id.'/feed', 'post', array('message'=> $status, 'cb' => ''));

		
		}
		                
			  //$attachment = array(
								//  'name' => 'Crush On You',
								//  'description' => 'One Of your friend has crush on you',
								//  'picture' => '',
								//  'link' => 'http://duminda.net76.net/'
							  
								 // );

			 //$facebook->api('/'.$me['id'].'/feed', 'post', $attachment );
	/********************************************************/
		
	
	
	
	
	
	


	

?>


<input type="button" onclick="sendRequest();" value="send Request" />

  <div id="fb-root"></div>
  <script src="http://connect.facebook.net/en_US/all.js">
  </script>
  <script>
  function sendRequest(){

    FB.init({
      appId:'257478380945147', cookie:true,
      status:true, xfbml:true
    });

    FB.ui({ method: 'apprequests',
      message: 'Here is a new Requests dialog...'});

	}


  </script>
